package com.wzdigit.wms.raw.service.backtosap;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.mid.IfPostback;
import com.wzdigit.wms.basic.domain.transfer.GdnDtl;
import com.wzdigit.wms.basic.domain.transfer.GdnHeader;
import com.wzdigit.wms.basic.enums.writeback.BackResultEnum;
import com.wzdigit.wms.basic.enums.writeback.OtherSystemEnum;
import com.wzdigit.wms.basic.enums.writeback.TopicEnum;
import com.wzdigit.wms.common.entity.StatusCode;
import com.wzdigit.wms.common.utils.SapWebServiceUtil;
import com.wzdigit.wms.raw.exception.SystemException;
import com.wzdigit.wms.raw.service.mid.IfPostbackService;
import com.wzdigit.wms.web.service.common.wms2sap.ZDT_MsgHeader;
import com.wzdigit.wms.web.service.wms2sap.wmsbillstatus.ZDT_076_Req;
import com.wzdigit.wms.web.service.wms2sap.wmsbillstatus.ZDT_076_ReqHeader;
import com.wzdigit.wms.web.service.wms2sap.wmsbillstatus.ZDT_076_ReqItems;
import com.wzdigit.wms.web.service.wms2sap.wmsbillstatus.ZDT_076_RspRecord;
import com.wzdigit.wms.web.service.wms2sap.wmsbillstatus.ZSI_076_WMS2SAP_OUTProxy;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
  * WMS单据状态回写
  * @return
  * @author shuaic
  * @date 2021/6/18 18:15
*/
@Service
@Slf4j
public class WmsBillStatusBackService {

    @Autowired
    private IfPostbackService ifPostbackService;

    @Value("${sap.web.service.protocol-head}")
    private String protocolHead;

    @Value("${sap.web.service.wmsBillStatus-url}")
    private String uri;

    @Value("${wzdigit.web.service.sap.ip}")
    private String ip;
    
    /**
	 * 回传给SAP
	 * 
	 * @param list
	 * @return java.lang.String
	 * @author shuaic
	 * @date 2021/8/2 19:49
	 */
	public void backToSap(ZDT_076_ReqItems item) throws RemoteException {
		log.info("调用SAP单据状态回写接口开始--------");
		log.info("发送的报文===》" + item);
		
		String urlString = protocolHead + ip + uri;
		
		ZDT_076_Req req = new ZDT_076_Req();
		
		ZDT_MsgHeader header = SapWebServiceUtil.getHeader();
		ZDT_076_ReqHeader zdt_076_ReqHeader = new ZDT_076_ReqHeader(header);
		req.setHeader(zdt_076_ReqHeader);
		
		ZDT_076_ReqItems[] records = new ZDT_076_ReqItems[]{item};
		req.setItems(records);
		
		ZSI_076_WMS2SAP_OUTProxy proxy = new ZSI_076_WMS2SAP_OUTProxy();
		proxy.setEndpoint(urlString);
		
		ZDT_076_RspRecord[] rsp = null;// 回参
		try {
			// 调接口
			rsp = proxy.ZSI_076_WMS2SAP_OUT(req);
			log.info("回传的报文===》" + JSON.toJSONString(rsp));
			log.info("调用SAP单据状态回写接口结束--------");
		} catch (Exception e) {
			log.info("调用SAP单据状态回写接口异常--------");
			e.printStackTrace();
			throw e;
		}
		
		if (!"S".equals(rsp[0].getRESPONSE())) {
			String msg = rsp[0].getREMARK();
			if(StringUtils.isBlank(msg)) {
				msg = "处理失败";
			}
			throw new SystemException(String.format("SAP反馈：%s", msg));
		}
	}
	

	/*  
     * WMS单据状态回写 ASN
     * 
     * @param header 	表头
     * @param dtl 		明细
     * @param status 	状态
     * @param username 	更改人
     * @return boolean
     * @author shuaic
     * @date 2021/6/18 9:57
     */
	public void wmsBillStatusWriteBack(AsnHeader header, List<AsnDtl> dtlList, String status, String userName) {
		// 生成回写数据
		List<ZDT_076_ReqItems> list = new ArrayList<>();
		for (AsnDtl dtl : dtlList) {
			ZDT_076_ReqItems record = new ZDT_076_ReqItems();
			record.setBILLTYPE("1");				// 单据类型 ：1交货单、2预留单、3报废单
			record.setBILLNO(header.getAsnNo());	// 单号
			record.setZDATE(DateUtil.format(new Date(), "yyyyMMdd")); 	// 传输日期
			record.setZTIME(DateUtil.format(new Date(), "HHmmss")); 		// 传输时间
			record.setSTATUS(status);				// 1 拣配、2取消拣配
			record.setZNAME(userName);

			list.add(record);
		}
		String jsonString = JSON.toJSONString(list);

		// 插入数据到待回传数据表
		IfPostback ifPostback = new IfPostback();
		ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
		ifPostback.setPostbackTopic(TopicEnum.TOPIC_SAP_WMS_BILL_STATUS.getEnumName());
		ifPostback.setPostbackContent(jsonString);
		ifPostback.setPostbackResult(BackResultEnum.DEFAULT_STATE.getCode());
		ifPostback.setCreateTime(new Date());
		ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
		try {
			ifPostbackService.addOne(ifPostback);	// 保存
		} catch (Exception e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_MODIFY_ERROR);
		}
	}

    /*  
     * WMS单据状态回写 GDN
     * 
     * @param header 	表头
     * @param dtl 		明细
     * @param status 	状态
     * @param username 	更改人
     * @return boolean
     * @author shuaic
     * @date 2021/6/18 9:57
     */
	public void wmsBillStatusWriteBack(GdnHeader header, List<GdnDtl> dtlList, String status, String userName) {
		// 生成回写数据
		List<ZDT_076_ReqItems> list = new ArrayList<>();
		for (GdnDtl dtl : dtlList) {
			ZDT_076_ReqItems record = new ZDT_076_ReqItems();
			record.setBILLTYPE("1");				// 单据类型 ：1交货单、2预留单、3报废单
			record.setBILLNO(header.getGdnNo());	// 单号
			record.setZDATE(DateUtil.format(new Date(), "yyyyMMdd")); 	// 传输日期
			record.setZTIME(DateUtil.format(new Date(), "HHmmss")); 		// 传输时间
			record.setSTATUS(status);				// 1 拣配、2取消拣配
			record.setZNAME(userName);

			list.add(record);
		}
		String jsonString = JSON.toJSONString(list);

		// 插入数据到待回传数据表
		IfPostback ifPostback = new IfPostback();
		ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
		ifPostback.setPostbackTopic(TopicEnum.TOPIC_SAP_WMS_BILL_STATUS.getEnumName());
		ifPostback.setPostbackContent(jsonString);
		ifPostback.setPostbackResult(BackResultEnum.DEFAULT_STATE.getCode());
		ifPostback.setCreateTime(new Date());
		ifPostback.setDestSystem(OtherSystemEnum.SYSYTEM_SAP.getEnumName());
		try {
			ifPostbackService.addOne(ifPostback);	// 保存
		} catch (Exception e) {
			e.printStackTrace();
			Shift.fatal(StatusCode.SERVER_MODIFY_ERROR);
		}
	}
}